package com.helen.ui.login;

import android.content.Intent;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.coorchice.library.SuperTextView;
import com.helen.db.UserHelper;
import com.helen.global.Constants;
import com.helen.shopping.R;
import com.helen.ui.BaseActivity;
import com.helen.utils.ActivityHelper;
import com.helen.utils.MD5;
import com.helen.utils.MyLog;
import com.helen.utils.PhoneUtils;
import com.helen.utils.SharedPreferencesUtils;
import com.helen.utils.tiputils.MToast;
import com.helen.widget.MyEditText;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.HttpParams;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by yang on 2018/4/16.
 * 密码找回
 */

public class FindPasswordActivity extends BaseActivity {
    @BindView(R.id.et_phone)
    MyEditText etPhone;
    @BindView(R.id.et_sms_code)
    MyEditText etSmsCode;
    @BindView(R.id.tv_get_sms_code)
    TextView tvGetSmsCode;
    @BindView(R.id.et_password)
    MyEditText etPassword;
    @BindView(R.id.et_password_again)
    MyEditText etPasswordAgain;
    @BindView(R.id.tv_commit)
    SuperTextView tvCommit;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_find_password;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        getToolbarTitle().setText(R.string.find_password);
    }

    @Override
    protected void updateViews() {

    }


    @OnClick({R.id.tv_get_sms_code, R.id.tv_commit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_get_sms_code:
                String phoneCode = etPhone.getText().toString().trim();
                if (TextUtils.isEmpty(phoneCode)) {
                    MToast.makeTextShort(FindPasswordActivity.this, "手机号不能为空").show();
                    etPhone.requestFocus();
                    return;
                }

                if (!PhoneUtils.isMobileNO(phoneCode)) {
                    MToast.makeTextShort(FindPasswordActivity.this, "手机号格式错误").show();
                    etPhone.requestFocus();
                    return;
                }
                getMsgCodeApi(phoneCode);

                break;
            case R.id.tv_commit:
                String phone = etPhone.getText().toString().trim();
                String code = etSmsCode.getText().toString().trim();
                String pwd = etPassword.getText().toString().trim();
                String pwdAgain = etPasswordAgain.getText().toString().trim();
                if (TextUtils.isEmpty(phone)) {
                    MToast.makeTextShort(FindPasswordActivity.this, "手机号不能为空").show();
                    etPhone.requestFocus();
                    return;
                }

                if (!PhoneUtils.isMobileNO(phone)) {
                    MToast.makeTextShort(FindPasswordActivity.this, "手机号格式错误").show();
                    etPhone.requestFocus();
                    return;
                }
                if (TextUtils.isEmpty(code)) {
                    MToast.makeTextShort(FindPasswordActivity.this, "验证码不能为空").show();
                    etSmsCode.requestFocus();
                    return;
                }
                if (TextUtils.isEmpty(pwd)) {
                    MToast.makeTextShort(FindPasswordActivity.this, "密码不能为空").show();
                    etPassword.requestFocus();
                    return;
                }
                if (TextUtils.isEmpty(pwdAgain)) {
                    MToast.makeTextShort(FindPasswordActivity.this, "再次输入密码不能为空").show();
                    etPasswordAgain.requestFocus();
                    return;
                }

                if (!pwd.equals(pwdAgain)) {
                    MToast.makeTextShort(FindPasswordActivity.this, "两次输入的密码不一致").show();
                    etPasswordAgain.requestFocus();
                    return;
                }
                UpdatePwdApi(phone, code, pwd);
                break;
        }
    }

    private void UpdatePwdApi(String phone, String code, String pwd) {
        mMProgressDialog.show();
        HttpParams params = new HttpParams();
        params.put("phone", phone);
        params.put("code", code);
        params.put("pwd", pwd);
        EasyHttp.post(Constants.API_JSONAPI_UPDATEPWD)
                .params(params)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        mMProgressDialog.dismiss();
                    }

                    @Override
                    public void onError(ApiException e) {
                        MyLog.d(TAG, "修改密码失败==" + e.toString());
                        MToast.makeTextShort(FindPasswordActivity.this, getString(R.string.service_error)).show();
                    }

                    @Override
                    public void onSuccess(String result) {
                        String data = PhoneUtils.checkResponseFlag(FindPasswordActivity.this, result, TAG);
                        if (data != null) {
                            MToast.makeTextShort(FindPasswordActivity.this, "密码修改成功,请重新登录").show();
//                            SharedPreferencesUtils.remove(UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_AUTH_KEY_STORAGE);
//                            SharedPreferencesUtils.remove(UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_LOGIN_MODEL);
                            ActivityHelper.getInstance().finishAllActivity();
                            UserHelper.getInstance().setloginEntity(FindPasswordActivity.this, null);
                            UserHelper.getInstance().setIsLogined(false);
                            startActivity(new Intent(FindPasswordActivity.this, LoginActivity.class));
                        }
                    }
                });
    }


    //    获取验证码接口
    private void getMsgCodeApi(String phone) {
        mMProgressDialog.show();
        HttpParams params = new HttpParams();
        params.put("id", phone);
        params.put("key", MD5.getMD5Str(phone + "888888").toUpperCase());
        EasyHttp.post(Constants.API_JSONAPI_SMS_CODE)
                .params(params)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        mMProgressDialog.dismiss();
                    }

                    @Override
                    public void onError(ApiException e) {
                        MyLog.d(TAG, "获取验证码失败==" + e.toString());
                        MToast.makeTextShort(FindPasswordActivity.this, getString(R.string.service_error)).show();
                    }

                    @Override
                    public void onSuccess(String result) {
                        String data = PhoneUtils.checkResponseFlag(FindPasswordActivity.this, result, TAG);
                        if (data != null) {
                            timer.start();
                        }

                    }
                });
    }

    //   倒计时响应事件  timer.start();
    private CountDownTimer timer = new CountDownTimer(60000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            tvGetSmsCode.setClickable(false);// 设置不能点击
            tvGetSmsCode.setText(millisUntilFinished / 1000 + "秒后重新发送");// 设置倒计时时间
            tvGetSmsCode.setTextColor(getResources().getColor(R.color.appTipColor)); // 设置为灰色，这时是不能点击的
        }

        @Override
        public void onFinish() {
            tvGetSmsCode.setText(getResources().getString(R.string.get_sms_code));
            tvGetSmsCode.setClickable(true);// 重新获得点击
            tvGetSmsCode.setTextColor(getResources().getColor(R.color.appThemeColor));

        }
    };
}
